
$(function(){
	$("#doRecharge").on("click",doRecharge);
	$("#rechargeNum").on("change",setRechargeValues);
	$(".isVoucher").on("click",useVoucher);
	$('#voucherNum').on("blur",setVoucher);
	$('.sta').on('click',setRechargeValues);
});

//代金券查询
function setVoucher(){
	if($('#voucherNum').prop("readOnly")) return;
	var voucherNum = $('#voucherNum').val();
	voucherNum = $.trim(voucherNum);
	if(voucherNum == "") {
		BSTYUN.dialog.ok('请输入代金券编号！');
		return;
	}
	$.ajax({
		type:"POST",
		url:"/buy/checkVoucherNum",
		async:false,
		data:{
			voucherNum:voucherNum,
			dtype:'cz',
			bType:''
		},
		success:function(data,response){
			
			if(data.vIsExist == true){
				if(data.vTypeFlag == true){
					if(data.bTypeFlag == true){
						if(data.bInfoFlag == true){
							var voucher = data.voucher;
							$('#voucherMoney')[0].innerHTML = voucher.voucherMoney;
							$('#voucherMoney').html(parseFloat(voucher.voucherMoney).toFixed(2));
							calculateRechargeNum();
						}else{
							BSTYUN.dialog.ok("该代金券绑定信息与当前用户信息不一致！请输入正确的代金券编号！");
							$('#voucherNum').val('');
							$('#voucherMoney').html(0.00);
						}
					}else{
						BSTYUN.dialog.ok("该代金券绑定类型与当前用户类型不一致！请输入正确的代金券编号！");
						$('#voucherNum').val('');
						$('#voucherMoney').html(0.00);
					}
				}else{
					BSTYUN.dialog.ok("该代金券类型与当前使用类型不一致！请输入正确的代金券编号！");
					$('#voucherNum').val('');
					$('#voucherMoney').html(0.00);
				}
			}else{
				BSTYUN.dialog.ok("该代金券不存在！请输入正确的代金券编号！");
				$('#voucherNum').val('');
				$('#voucherMoney').html(0.00);
			}
		}
	})
}

//计算
function calculateRechargeNum(){
	var rechargeNum = $('input[name="rechargeNum"]').val();
	var voucherMoney = $('#voucherMoney')[0].innerHTML;
	
	if(Number(rechargeNum) <= Number(voucherMoney)){
		
		$('#rechargeNumber')[0].innerHTML = 0.00;
	}else{
		var actualPay = Number(rechargeNum) - Number(voucherMoney);
		$('#rechargeNumber')[0].innerHTML = parseFloat(actualPay).toFixed(2);
	}
}

//使用代金券
function useVoucher(){
	var $this = $(this);
	if(!$this.prop("checked")){
		$('#voucherNum').prop("readOnly",true);
		$('#voucherNum').val('');
		$('#voucherMoney').html(0.00);
		var voucherMoney = 0.00;
		$('#showVoucherMoney').prop("hidden",true);
	}else{
		$('#voucherNum').prop("readOnly",false);
		$('#voucherNum').val('');
		$('#showVoucherMoney').prop("hidden",false);
	}
}

//验证金额
function setRechargeValues(){
	var rechargeNum = $('input[name="rechargeNum"]').val();
	rechargeNum = Number(rechargeNum);
	var reg = /^([1-9]\d*\.\d*|0\.\d+|[1-9]\d*|0)$/;
			   
	if(rechargeNum == '' || reg.test(rechargeNum) == false) {
		$('#rechargeNumber').text(0);
		BSTYUN.dialog.warn('请输入正确的金额！');
		return false;
	}
	rechargeNum = parseFloat(rechargeNum).toFixed(2);
	
	$('#rechargeNumber').text(rechargeNum);
	return true;
};

//充值
function doRecharge(){
	var voucherNum = $('#voucherNum').val();
	voucherNum = $.trim(voucherNum);
	
	if(voucherNum == '' || voucherNum == null){
		voucherNum = '0';
	}
	
	var userId = $('input[name="userId"]').val();
	var voucherMoney = $('#voucherMoney')[0].innerHTML;
	var rechargeNum = $('input[name="rechargeNum"]').val();
	rechargeNum = Number(rechargeNum);
	var reg = /^([1-9]\d*\.\d*|0\.\d+|[1-9]\d*|0)$/;
			   
	if(rechargeNum == '' || reg.test(rechargeNum) == false) {
		BSTYUN.dialog.warn('请输入正确的金额！');
		return;
	}
	rechargeNum = parseFloat(rechargeNum).toFixed(2);
	
	if(Number(rechargeNum) < Number(voucherMoney)){
		rechargeNum = 0.00;
	}else{
		rechargeNum = Number(rechargeNum) - Number(voucherMoney);
	}
	
	var $alipayRadio = $("#alipayRadio");
	var $chinapayRadio = $("#chinapayRadio");
	var $chinapayB2BRadio = $("#chinapayB2BRadio");
	var $chinapayB2CRadio = $("#chinapayB2CRadio");
	
	if(Number(rechargeNum) == 0){
		
		rechargeAccount();
		
	}else{
		if($alipayRadio.prop("checked")){ //修改user余额   增加deal表
			location.href="/alipay/doRecharge?userId="+userId+"&rechargeNum="+parseFloat(rechargeNum).toFixed(2)+"&voucherNum="+voucherNum;
		}else if($chinapayRadio.prop("checked")){
			location.href="/chinapay/rechargeByNoCard?userId="+userId+"&rechargeNum="+parseFloat(rechargeNum).toFixed(2)+"&voucherNum="+voucherNum;
		
		}
		else if($chinapayB2BRadio.prop("checked")){
			location.href="/chinapay/rechargeByB2B?userId="+userId+"&rechargeNum="+parseFloat(rechargeNum).toFixed(2)+"&voucherNum="+voucherNum;
		
		}else if($chinapayB2CRadio.prop("checked")){
			location.href="/chinapay/rechargeByB2C?userId="+userId+"&rechargeNum="+parseFloat(rechargeNum).toFixed(2)+"&voucherNum="+voucherNum;
		
		}
	}
	
}

function rechargeAccount(){
	var userId = $('input[name="userId"]').val();
	var voucherNum = $('#voucherNum').val();
	voucherNum = $.trim(voucherNum);
	$.ajax({
		type:"GET",
		url:"/finance/doRecharge",
		async:false,
		data:{
			userId:userId,
			rechargeNum:$.trim($('input[name="rechargeNum"]').val()),
			voucherNum:voucherNum
		},
		success:function(data,response){
			if(data){
				BSTYUN.dialog.ok("余额已充值成功！",function(){
					location.href ="/finance/myAccount";
				});
			}else{
				BSTYUN.dialog.warn("余额已充值失败，请重新充值！");
			}
		}
	})
}

//判断邮箱
var isEmail = function(str) {
        var emailReg = /^[a-z0-9][\w\.]*@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,5}$/i;
        return emailReg.test(str);
    }

var isMobile = function(str){
	var mobileReg = /^1[345678][0-9]{9}$/;
	return mobileReg.test(str);
}


$(function(argument) {
	var $next = $("#next"),
		$email = $(".email"),
		$company = $(".company"),
		$rechargeNumber = $("#rechargeNumber"),
		$voucherMoney = $('#voucherMoney'),
		$linkMan = $(".linkMan"),
		$linkPhone = $(".linkPhone"),
		$panel = $(".panel"),
		$back = $("#back"),
		$submit = $("#submit");
	$next.add($back).on("click",function(){
		$panel.toggle();
	});
	//线下支付
	$submit.on('click', function(event) {
		if(!setRechargeValues()){
			return BSTYUN.dialog.warn("请输入正确的金额！");
		};
		var email = $.trim($email.val()),
			company = $.trim($company.val()),
			rechargeNumber = $.trim($rechargeNumber[0].innerHTML),
			linkMan = $.trim($linkMan.val()),
			linkPhone = $.trim($linkPhone.val()),
			length = company.length,
			voucherMoney = $voucherMoney[0].innerHTML;
		var voucherNum = $('#voucherNum').val();
		voucherNum = $.trim(voucherNum);
		if(voucherNum == '' || voucherNum == null ){
			voucherNum = '';
		}
		if(Number(rechargeNumber) == 0){
			rechargeAccount();
		}else{
			
			if (length == 0) {
				$('.domain_error').eq(0).html('单位全称不能为空!');
				$('.domain_error').eq(1).html('');
				$('.domain_error').eq(2).html('');
				$('.domain_error').eq(3).html('');
				return false;
			}else{
				
			}
			
			lm_length = linkMan.length;
			if (lm_length == 0) {
				$('.domain_error').eq(0).html('');
				$('.domain_error').eq(1).html('联系人不能为空');
				$('.domain_error').eq(2).html('');
				$('.domain_error').eq(3).html('');
				return false;
			}else{
				
			}
			
			if (isMobile(linkPhone)) {
				
			}else{
				$('.domain_error').eq(0).html('');
				$('.domain_error').eq(1).html('');
				$('.domain_error').eq(2).html('手机号码格式错误');
				$('.domain_error').eq(3).html('');
				return false;
			}
			
			
			if (isEmail(email)) {
	
			}else{
				$('.domain_error').eq(0).html('');
				$('.domain_error').eq(1).html('');
				$('.domain_error').eq(2).html('');
				$('.domain_error').eq(3).html('邮箱格式错误');
				return false;
			}
		
			//发送邮件给客户    
			$.ajax({
				type:"POST",
				url:"/finance/rechargeSendEmailToUser",
				async:false,
				data:{
					email:email,
					company:company,
					rechargeNumber:$.trim($('input[name=rechargeNum]').val()),
					linkMan:linkMan,
					linkPhone:linkPhone,
					voucherNum:voucherNum 
				},
				success:function(data,response){
					if(data){
						BSTYUN.dialog.ok("您的资料已提交，请按照提交内容进行付款，款到后次日五点前将充值到您的现金账户余额，谢谢！<br>如有疑问请拨打我们的服务电话：400-8078-100",function(){
							location.href ="/finance/trade";
						});
					}else{
						BSTYUN.dialog.warn("邮件发送失败，请重新发送！");
					}
				}
			})
		}
	});

});